package com.hoko.blur.processor;

import ohos.media.image.PixelMap;

import com.hoko.blur.opengl.offscreen.EglBuffer;
import com.hoko.blur.util.Preconditions;

/**
 * 打开glblur处理器
 *
 * @author
 * @date 2021/08/03
 */
class OpenGLBlurProcessor extends BlurProcessor {
    /**
     * 标签
     */
    private static final String TAG = OpenGLBlurProcessor.class.getSimpleName();

    /**
     * m egl缓冲
     */
    private final EglBuffer mEglBuffer;

    /**
     * 打开glblur处理器
     *
     * @param builder 构建器
     */
    OpenGLBlurProcessor(HokoBlurBuild builder) {
        super(builder);
        mEglBuffer = new EglBuffer();

    }

    /**
     * 做内部模糊
     *
     * @param scaledInBitmap 相应的位图
     * @param concurrent 并发
     * @return {@link PixelMap}
     */
    @Override
    protected PixelMap doInnerBlur(PixelMap scaledInBitmap, boolean concurrent) {
        Preconditions.checkNotNull(scaledInBitmap, "scaledInBitmap == null");
        Preconditions.checkArgument(!scaledInBitmap.isReleased(), "You must input an unrecycled bitmap !");

        // TODO: 2017/2/20 opengl process parallel
        mEglBuffer.setBlurRadius(mRadius);
        mEglBuffer.setBlurMode(mMode);
        return mEglBuffer.getBlurBitmap(scaledInBitmap);
    }

    /**
     * 免费的
     */
    @Override
    protected void free() {
        mEglBuffer.free();
    }

    /**
     * 完成
     *
     * @throws Throwable throwable
     */
    @Override
    protected void finalize() throws Throwable {
        free();
        super.finalize();
    }
}